
export function inArray(arr,val){
	for(let i=0;i<arr.length;i++){
		if(arr[i]==val){
			return true;
		}
	}
	return false;
}

export function formatDate(dateOryear,month,day){
  if(!month && !day){
	  let date=dateOryear;
	  let year = date.getFullYear();
	  let month = date.getMonth() + 1;
	  if(month<10){
	    month='0'+month;
	  }
	  let day = date.getDate();
	  if(day<10){
	    day='0'+day;
	  }
	  return year+'-'+month+'-'+day;
  }else{
	 let year = dateOryear;
	 month = month + 1;
	 if(month<10){
	   month='0'+month;
	 }
	 if(day<10){
	   day='0'+day;
	 }
	 return year+'-'+month+'-'+day;
  } 
}

export function formatDateTime(date){
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  if(month<10){
    month='0'+month;
  }
  let day = date.getDate();
  if(day<10){
    day='0'+day;
  }
  let hour=date.getHours();
  if(hour<10){
    hour='0'+hour;
  }
  let minis=date.getMinutes();
  if(minis<10){
    minis='0'+minis;
  }
  let seconds=date.getSeconds();
  if(seconds<10){
    seconds='0'+seconds;
  }
  return year+'-'+month+'-'+day+' '+hour+':'+minis+':'+seconds;
}

export function rand(n,m){
	return Math.floor(Math.random() * (m - n + 1)) + n;
}

export function parseNumber(number){
	if(parseInt(number, 10) != number){
		return Number(parseFloat(number).toFixed(2));
	}
	return parseInt(number);
}

export function getUniqid() {
    var currentDate = new Date();
    var timestamp = currentDate.getTime().toString(36); // 使用36进制
    var unique = (currentDate.getUTCMilliseconds() + Math.random()).toString(36).slice(2, 7); // 随机数
    return (timestamp + unique).replace('.','');
}

export function formatDuration(seconds,endtype='seconds') {
	seconds=parseInt(seconds);
    const days = Math.floor(seconds / (24 * 60 * 60));
    const hours = Math.floor((seconds % (24 * 60 * 60)) / (60 * 60));
    const minutes = Math.floor((seconds % (60 * 60)) / 60);
    const remainingSeconds = seconds % 60;
    let formattedDuration = "";
	if(parseInt(seconds)<=0){
		return '0秒';
	}
    if (days > 0) {
        formattedDuration += `${days}天`;
		if(endtype=='days' && formattedDuration){
			return formattedDuration.trim();
		}
    }
    if (hours > 0) {
        formattedDuration += ` ${hours}小时`;
		if(endtype=='hours' && formattedDuration){
			return formattedDuration.trim();
		}
    }
    if (minutes > 0) {
        formattedDuration += ` ${minutes}分`;
		if(endtype=='minutes' && formattedDuration){
			return formattedDuration.trim();
		}
    }
    if (remainingSeconds > 0) {
        formattedDuration += ` ${remainingSeconds}秒`;
		if(endtype=='seconds' && formattedDuration){
			return formattedDuration.trim();
		}
    }
    return formattedDuration.trim();
}
